package cn.fzhui;

/**
 * 棋盘类
 */
public class Chessboard {


    public static final int BOARD_SIZE = 16;

    private final String element = " + ";

    private String[][] board = new String[Chessboard.BOARD_SIZE][Chessboard.BOARD_SIZE];

    private String[][] printBoard = new String[Chessboard.BOARD_SIZE + 1][Chessboard.BOARD_SIZE + 1];

    // 当前棋手
    private Chessman currentChessman = Chessman.BLACK;

    /**
     * 新建对象时，初始化棋盘
     */
    Chessboard() {
    }

    /**
     * 初始化棋盘
     */
    public void initBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = element;
            }
        }
    }


    /**
     * 打印棋盘
     */
    public void printBoard() {
        this.deviation();

        for (int i = 0; i < this.printBoard.length; i++) {
            for (int j = 0; j < this.printBoard.length; j++) {
                System.out.print(this.printBoard[i][j]);
            }
            System.out.println();
        }
    }

    /**
     * 落子，设置元素
     */
    public boolean setElement(int row, int column) {
        // 判断对应格子是否有棋子，没有则将对应单元格，设置为对应的棋子
        if (this.board[row][column].equals(this.element)) {
            this.board[row][column] = this.currentChessman.getChessman();
            return true;
        }
        System.out.println(row + "," + column + "位置已经有棋子了！！！");
        return false;
    }

    /**
     * 数组偏移，并复制
     */
    private void deviation() {
        String fill = "   ";
        for (int i = 0; i < this.printBoard.length; i++) {
            for (int j = 0; j < this.printBoard.length; j++) {

                // 初始化行标
                if (i == 0 && j != 0) {
                    fill = " " + Integer.toHexString(j - 1) + " ";
                }

                // 初始化列标
                if (i != 0 && j == 0) {
                    fill = " " + Integer.toHexString(i - 1) + " ";
                }

                // 棋盘格局
                if (i > 0 && j > 0) {
                    fill = this.board[i - 1][j - 1];
                }

                this.printBoard[i][j] = fill;

            }
        }
    }

    /**
     * 开始游戏
     */
    public void start() {
        this.initBoard();
    }

    /**
     * 请落子
     */
    public void printPlease() {
        System.out.println("\n 请\"" + this.currentChessman.getChessman() + "\"方落子，以\"x,y\"格式选择落子位置：");
    }


    /**
     * 变更当前棋手
     */
    public void changeChessman() {

        if (this.currentChessman.getChessman().equals(Chessman.BLACK.getChessman())) {
            this.currentChessman = Chessman.WHITE;
        } else {
            this.currentChessman = Chessman.BLACK;

        }
    }

    /**
     * 是否胜出
     *
     * @return
     */
    public boolean isWon(int row, int column) {

        int count;

        // 胜出需要多少个棋子
        int winCount = 5;

        for (int i = 0; i < this.board.length; i++) {
            for (int j = 0; j < this.board.length; j++) {

                String chessman = this.board[i][j];

                // 打竖
                count = 0;
                if (column == j) {
                    if (chessman.equals(this.currentChessman.getChessman())) {
                        count++;
                        if (count == winCount) {
                            this.printWinMessage();
                            return true;
                        }
                    } else {
                        count = 0;
                    }
                }

                // 打横
                count = 0;
                if (row == i) {
                    if (chessman.equals(this.currentChessman.getChessman())) {
                        count++;
                        if (count == winCount) {
                            this.printWinMessage();
                            return true;
                        }
                    } else {
                        count = 0;
                    }
                }
            }
        }

        // 右上到左下
        count = 0;
        for (int c = 0; c < BOARD_SIZE; c++) {
            for (int r = 0, c2 = c; c2 >= 0; r++, c2--) {
                if (this.board[r][c2].equals(this.currentChessman.getChessman())) {
                    count++;
                    if (count == winCount) {
                        this.printWinMessage();
                        return true;
                    }
                } else {
                    count = 0;
                }
            }
        }

        // 左上到右下
        count = 0;
        for (int c = 5; c >= 0; c--) {
            for (int r = 0, c2 = c; c2 < BOARD_SIZE; r++, c2++) {
                if (this.board[r][c2].equals(this.currentChessman.getChessman())) {
                    count++;
                    if (count == winCount) {
                        this.printWinMessage();
                        return true;
                    }
                } else {
                    count = 0;
                }
            }
        }

        return false;
    }

    /**
     * 打印胜出信息
     */
    private void printWinMessage() {
        this.printBoard();
        System.out.println("\n恭喜" + this.currentChessman.getChessman() + "方胜出！！！！！！");
    }


}